//
//  AreaService.m
//  bestkeep
//
//  Created by bj on 2016/11/14.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BK_my_AreaService.h"
#import "BK_my_Area.h"
#import "RequestManager.h"

@implementation BK_my_AreaService

NSString * const formalEnvironmentAreaStrBKAPI = @"http://api.bestkeep.cn/";

NSString * const bk_my_areaStrBKAPI = @"http://api.test.bestkeep.cn/";

//查询地区/省市接口
NSString * const strDeliver_all =@"/area/getAllAreaInfo";

//查询省份接口
NSString * const strArea_province = @"area/province";

//查询城市接口
NSString * const strArea_city = @"area/city";

//查询区县接口
NSString * const strArea_county = @"area/county";

NSString *const strCheck_version = @"version/ios-p";

NSString *const strMenber_bind = @"member/bind";

NSString *const strConfirm_receive = @"order/confirmOrder";


+ (NSString *)formalEnvironment:(NSString *)url {
    if (ApplicationEnvironmentProduction == 1) {
        return [formalEnvironmentAreaStrBKAPI stringByAppendingString:url];
    }
    return [bk_my_areaStrBKAPI stringByAppendingString:url];
}

+ (void)searchAllAreaInfo:(NSString *)all_areaInfo callback:(AreaCallback)callback {
    NSString *url = [self formalEnvironment:strDeliver_all];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:nil success:^(id  _Nullable responseObject) {
        NSDictionary *dict = responseObject;
        BOOL success = [[dict objectForKey:@"success"] boolValue];
        if (success) {
            callback(dict, nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

//查询地区／省份
+ (void)searchRegionsCallback:(AreaCallback)callback {
    NSString *url = [self formalEnvironment:strArea_province];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:nil success:^(id  _Nullable responseObject) {
        NSDictionary *dict = responseObject;
        BOOL success = [[dict objectForKey:@"success"] boolValue];
        if (success) {
            NSDictionary *data = [dict objectForKey:@"data"];
            NSArray *list = [data objectForKey:@"list"];
            NSMutableArray *muArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < list.count; i++) {
                NSDictionary *area_dic = list[i];
                BK_my_Area *area = [[BK_my_Area alloc] init];
                area.areaCode = [area_dic objectForKey:@"code"];
                area.areaName = [area_dic objectForKey:@"name"];
                [muArray addObject:area];
            }
            callback(muArray, nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

+ (void)searchCity:(NSString *)province_code callback:(AreaCallback)callback {
    NSString *url = [self formalEnvironment:strArea_city];
    NSMutableDictionary *muDict = [[NSMutableDictionary alloc] init];
    [muDict setValue:province_code forKey:@"province_code"];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:muDict success:^(id  _Nullable responseObject) {
        NSDictionary *dict = responseObject;
        BOOL success = [[dict objectForKey:@"success"] boolValue];
        if (success) {
            NSDictionary *data = [dict objectForKey:@"data"];
            NSArray *list = [data objectForKey:@"list"];
            NSMutableArray *muArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < list.count; i++) {
                NSDictionary *area_dic = list[i];
                BK_my_Area *area = [[BK_my_Area alloc] init];
                area.areaCode = [area_dic objectForKey:@"code"];
                area.areaName = [area_dic objectForKey:@"name"];
                [muArray addObject:area];
            }
            callback(muArray, nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

+ (void)searchSquare:(NSString *)city_code  callback:(AreaCallback)callback {
    NSString *url = [self formalEnvironment:strArea_county];
    NSMutableDictionary *muDict = [[NSMutableDictionary alloc] init];
    [muDict setValue:city_code forKey:@"city_code"];
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:muDict success:^(id  _Nullable responseObject) {
        NSDictionary *dict = responseObject;
        BOOL success = [[dict objectForKey:@"success"] boolValue];
        if (success) {
            NSDictionary *data = [dict objectForKey:@"data"];
            NSArray *list = [data objectForKey:@"list"];
            
            NSMutableArray *muArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < list.count; i++) {
                NSDictionary *area_dic = list[i];
                BK_my_Area *area = [[BK_my_Area alloc] init];
                area.areaCode = [area_dic objectForKey:@"code"];
                area.areaName = [area_dic objectForKey:@"name"];
                area.areaMsg = [area_dic objectForKey:@"msg"];
                [muArray addObject:area];
            }
            callback(muArray, nil);
        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

@end
